package app;

import java.util.ArrayList;

/********************************************************
 * 
 * Project : TeamProject Package : app File : Recipe.java Name : Tyler Daines
 * Date : Nov 3, 2013
 * 
 ********************************************************/

public class Recipe implements Comparable<Recipe>
{
	private Integer re_uniqueID = -1;
	private String name;
	private ArrayList<Ingredient> ingredients;
	private String directions;
	private Float calories;
	private Float servingSize;
	private String picture;
	private boolean favorite;
	private Integer categoryID;
	private String category;

	// This creates a blank recipe
	public Recipe()
	{
		super();
		this.re_uniqueID = null;
		this.name = "New Recipe";
		this.ingredients = new ArrayList<Ingredient>();
		this.directions = "";
		this.calories = (float) 0;
		this.servingSize = (float) 0;
		this.picture = "/resources/recipes.png";
		this.favorite = false;
		this.categoryID = 0;
		this.category = "";
	}

	// This is for a new recipe to send TO the database
	public Recipe(String name, ArrayList<Ingredient> ingredients,
			String directions, float calories, float servingSize,
			String picture, boolean favorite, int categoryID, String category)
	{
		super();
		this.re_uniqueID = null;
		this.name = name;
		this.ingredients = ingredients;
		this.directions = directions;
		this.calories = calories;
		this.servingSize = servingSize;
		this.picture = picture;
		this.favorite = favorite;
		this.categoryID = categoryID;
		this.category = category;
	}

	// This is for a recipe being populated FROM the database
	public Recipe(int re_uniqueID, String name,
			ArrayList<Ingredient> ingredients, String directions,
			float calories, float servingSize, String picture,
			boolean favorite, int categoryID, String category)
	{
		super();
		this.re_uniqueID = re_uniqueID;
		this.name = name;
		this.ingredients = ingredients;
		this.directions = directions;
		this.calories = calories;
		this.servingSize = servingSize;
		this.picture = picture;
		this.favorite = favorite;
		this.categoryID = categoryID;
		this.category = category;

	}

	public int getRe_uniqueID()
	{
		if (re_uniqueID != null)
		{
			return re_uniqueID;
		}
		else
		{
			return -1;
		}
	}

	public void setRe_uniqueID(int re_uniqueID)
	{
		this.re_uniqueID = re_uniqueID;
	}

	public String getName()
	{
		if (name.equals(""))
		{
			return null;
		}
		else
		{
			return name;
		}
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public ArrayList<Ingredient> getIngredients()
	{
		return ingredients;
	}

	public void setIngredients(ArrayList<Ingredient> ingredients)
	{
		this.ingredients = ingredients;
	}

	public String getDirections()
	{
		if (directions == null)
		{
			return "";
		}
		else
		{
			return directions;
		}
	}

	public void setDirections(String directions)
	{
		this.directions = directions;
	}

	public float getCalories()
	{
		if (calories == null)
		{
			return 0;
		}
		else
		{
			return calories;
		}
	}

	public void setCalories(Float calories)
	{
		if (calories == null)
		{
			this.calories = (float) 0;
		}
		else
		{
			this.calories = calories;
		}
	}

	public float getServingSize()
	{
		if (servingSize == null)
		{
			return 0;
		}
		else
		{
			return servingSize;
		}
	}

	public void setServingSize(Float servingSize)
	{
		if (servingSize == null)
		{
			this.servingSize = (float) 0;
		}
		else
		{
			this.servingSize = servingSize;
		}
	}

	public String getPicture()
	{
		return picture;
	}

	public void setPicture(String picture)
	{
		this.picture = picture;
	}

	public boolean isFavorite()
	{
		return favorite;
	}

	public void setFavorite(boolean favorite)
	{
		this.favorite = favorite;
	}

	public Integer getCategoryID()
	{
		if (categoryID == null)
		{
			return 0;
		}
		else
		{
			return categoryID;
		}
	}

	public void setCategoryID(int categoryID)
	{
		this.categoryID = categoryID;
	}

	@Override
	public String toString()
	{
		return String.format("%-15s %-15s %-6s %-6s", category, name, calories, servingSize);
	}

	@Override
	public int compareTo(Recipe other)
	{
		int before = -1;
		int equal = 0;
		int after = 1;
		
		if(this == other)
			return equal;
		if(this.categoryID == other.categoryID)
		{
			if(this.name.equalsIgnoreCase(other.name))
			{
				if(this.calories == other.calories)
				{
					if(this.servingSize < other.servingSize)
						return after;
					else if(this.servingSize > other.servingSize)
						return before;
					else
						return equal;
				}
				else if(this.calories < other.calories)
					return after;
				else
					return before;
			}
			else if(this.name.compareToIgnoreCase(other.name) == -1)
				return before;
			else if(this.name.compareToIgnoreCase(other.name) == 1)
			{
				return after;
			}
			
		}
		else if(this.categoryID > other.categoryID)
			return after;
		else
			return before;
		return equal;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if (obj == this)
			return true;
		else if(!(obj instanceof Recipe))
				return false;
		Recipe other = (Recipe) obj;
		return (name == other.name) && (ingredients == other.ingredients) && 
				(directions == other.directions) && (calories == other.calories) &&
				(servingSize == other.servingSize) && (categoryID == other.categoryID);
	}
	
	@Override
	public int hashCode()
	{
		int result = 17;
		
		for(char ele : name.toCharArray())
			result += (int) ele;
		
		for(Ingredient ele: ingredients)
		{
			for(char ele1 : ele.getName().toCharArray())
				result += (int) ele1;
			result += ele.getAmount();
		}
		
		for(char ele : directions.toCharArray())
			result += (int) ele;
		
		for(char ele : directions.toCharArray())
			result += (int) ele;
		result += calories;
		result += servingSize;
		result += categoryID;
		result = 31 * result;
		return result;
	}

}
